package tools
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import mx.resources.ResourceBundle;
	import mx.utils.StringUtil;

	public class ResourceUtil extends EventDispatcher
	{
		private static var _instance:tools.ResourceUtil;
		private var _language:String;

		[ResourceBundle("sam_en_US")]
		private var lang_en:ResourceBundle;

		[ResourceBundle("sam_zh_CN")]
		private var lang_zh:ResourceBundle;

		[Bindable]
		private var localResources:ResourceBundle;

		public static function getInstance():tools.ResourceUtil
		{
			if (_instance == null)
			{
				_instance=new tools.ResourceUtil();
			}
			return _instance;
		}

		[Bindable(event="languageChange")]
		public function getString(key:String):String
		{
			return localResources.getString(key);
		}

		/**
		 * get the substitute properties by key and other parameter
		 */
		[Bindable(event="languageChange")]
		public function getSubstitute(key:String, ... rest):String
		{
			return StringUtil.substitute(localResources.getString(key), rest);
		}

		public function ResourceUtil()
		{
			this.language="en";
		}

		public function set language(language:String):void
		{
			this._language=language;

			if (_language == "en")
			{
				this.localResources=lang_en;
			}
			else if (_language == "zh")
			{
				this.localResources=lang_zh;
			}
			else
			{
				this.localResources=lang_en;
			}
			dispatchEvent(new Event("languageChange"));
		}
	}
}